<!-- 能力目标达成度 -->
<template>
  <div class="page-container">
    <div ref="chartContainer" class="chart-content"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'Capacity',
  data() {
    return {
      chart: null,
      colors: [
        '#0CD2E6',
        '#3751E6',
        '#FFC722',
        '#886EFF',
        '#008DEC',
        '#114C90',
        '#00BFA5'
      ],
      option: {
        color: this.colors,
        series: [{
          type: 'pie',
          radius: ['70%', '70%'],
          center: ['50%', '45%'],
          data: [],
          label: {
            show: true,
            position: 'outside',
            formatter: params => `${params.name}: {valueStyle|${params.value}}`,
            color: '#00eaff',
            fontSize: 12,
            fontWeight: 'bold',
            textShadow: '0 0 8px rgba(0, 234, 255, 0.8)',
            rich: {
              valueStyle: {
                color: '#ffffff',
                fontSize: 12,
                fontWeight: 'bold',
                textShadow: '0 0 5px rgba(255, 255, 255, 0.6)'
              }
            }
          },
          emphasis: {
            scale: false
          }
        }]
      }
    }
  },
  computed: {
    requestData() {
      return {
        missionType: this.$store.state.mission,
        studentClassId: this.$store.state.current,
        targetType: 1
      }
    }
  },
  watch: {
    requestData: {
      handler: 'fetchData',
      deep: true
    }
  },
  mounted() {
    this.fetchData()
    window.addEventListener('resize', this.handleResize)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.handleResize)
    this.chart?.dispose()
  },
  methods: {
    handleResize() {
      this.chart?.resize()
    },
    async fetchData() {
      try {
        const response = await this.$axios.post("/group/averageScoreByGroup", this.requestData)
        const data = response.data.data?.type?.[0] || []
        
        if (data.length) {
          this.option.series[0].data = this.formatChartData(data)
        } else {
          this.option.series[0].data = []
        }
        
        this.initChart()
      } catch (error) {
        console.error('获取数据失败', error)
        this.option.series[0].data = []
        this.initChart()
      }
    },
    formatChartData(data) {
      return data.flatMap((item, index) => [
        {
          value: item.score,
          name: `第${item.group}组`,
          itemStyle: {
            borderWidth: 10,
            shadowBlur: 20,
            borderColor: this.colors[index],
            shadowColor: this.colors[index]
          }
        },
        {
          name: '',
          itemStyle: {
            label: { show: false },
            labelLine: { show: false },
            color: 'rgba(0, 0, 0, 0)',
            borderColor: 'rgba(0, 0, 0, 0)',
            borderWidth: 0
          }
        }
      ])
    },
    initChart() {
      if (!this.chart) {
        this.chart = echarts.init(this.$refs.chartContainer)
      }
      this.chart.setOption(this.option)
    }
  }
}
</script>

<style scoped>
html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
}

.page-container {
  width: 100%;
  height: 80%;
}

.chart-content {
  width: 100%;
  height: 100%;
}
</style>